package com.ldc.wandroid.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;
import java.util.List;


public class ProjectTabAdapter extends FragmentStateAdapter {
    private final List<Fragment> fragmentList;

    public ProjectTabAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
        this.fragmentList = new ArrayList<>();
    }


    public void addFragment(Fragment fragment) {
        if (null == fragment) {
            return;
        }
        fragmentList.add(fragment);
        notifyDataSetChanged();
    }


    public void addFragments(List<Fragment> fragments) {
        for (Fragment fragment : fragments) {
            if (null == fragment) {
                continue;
            }
            addFragment(fragment);
        }
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        return fragmentList == null ? 0 : fragmentList.size();
    }
}
